package com.example.wuqujingdianphone.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.utils.permissionutil.PermissionListener;
import com.example.wuqujingdianphone.utils.permissionutil.PermissionUtil;

import java.util.List;


public class SplashActivity extends BaseActivity {
    public static int TIME = 1200; // 进入主程序的延迟时间
    public static final int SPLASH_PERMISSION_CODE = 1001;
    public String[] permissL = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_splash);
    }

    @Override
    protected void onResume() {
        super.onResume();
        openPermission();
    }

    private void into() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            }
        }, TIME);
    }


    private void openPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            initPermission();
        } else {
            into(); // 进入主程序的方法
        }
    }

    //Manifest.permission.CAMERA,Manifest.permission.CALL_PHONE
    public void initPermission() {
        PermissionUtil permissionUtil = new PermissionUtil(this);
        permissionUtil.requestPermissions(permissL, new PermissionListener() {
            @Override
            public void onGranted() {
                //所有权限都已经授权
                into(); // 进入主程序的方法
//                Toast.makeText(activity, "所有权限都已授权", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(List<String> deniedPermission) {
                //第一个被拒绝的权限
                showDialog1();
//                Toast.makeText(activity, "拒绝了权限" + deniedPermission.get(0), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onShouldShowRationale(List<String> deniedPermission) {
                //第一个勾选不在提示的权限
                showDialog1();
//                Toast.makeText(activity, "这个权限" + deniedPermission.get(0) + "勾选了不在提示，要像用户解释为什么需要这权限", Toast.LENGTH_LONG).show();
            }
        });
    }

    public void showDialog1() {
        new android.support.v7.app.AlertDialog.Builder(this)
                .setTitle(R.string.notifyTitle)
                .setMessage(R.string.notifyMsg)
                // 拒绝, 退出应用
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //AppManager.getAppManager().AppExit(BaseApplication.getInstance());
                            }
                        })
                .setPositiveButton(R.string.setting,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //获取应用详情页面intent
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                intent.setData(Uri.parse("package:" + getPackageName()));
                                startActivityForResult(intent, SPLASH_PERMISSION_CODE);
                            }
                        })
                .setCancelable(false)
                .show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SPLASH_PERMISSION_CODE) {
            if (ContextCompat.checkSelfPermission(getApplicationContext(), permissL[0]) != PackageManager.PERMISSION_GRANTED) {
                initPermission();
            } else {
                openPermission();
            }
        }
    }

}
